package com.coffee.takeoutserver.service.impl;

import com.coffee.takeoutserver.bean.User;
import com.coffee.takeoutserver.bean.UserExample;
import com.coffee.takeoutserver.dao.UserMapper;
import com.coffee.takeoutserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

@Override
public User getUserByPhone(String phone) {
    UserExample userExample = new UserExample();
    UserExample.Criteria criteria = userExample.createCriteria();
    criteria.andPhoneEqualTo(phone);
    List<User> users = userMapper.selectByExample(userExample);
    return users == null || users.size() == 0 ? null : users.get(0);
}

    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    @Override
public User loginByPhoneAndPassword(String phone, String password) {
    UserExample userExample = new UserExample();
    UserExample.Criteria criteria = userExample.createCriteria();
    criteria.andPhoneEqualTo(phone);
    criteria.andPasswordEqualTo(password);
    List<User> users = userMapper.selectByExample(userExample);
    return users == null || users.size() == 0 ? null : users.get(0);
}

    @Override
    public User getUserById(int id) {
        return userMapper.selectByPrimaryKey(id);
    }
}